﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace Server
{
    class CSocket
    {
        #region Bien
        TcpListener _server;
        List<TcpClient> _listClient;

        bool _isConnect = false;

        public bool IsConnect
        {
            get { return _isConnect; }
            set { _isConnect = value; }
        }

        public delegate void ChapNhanKetNoiHandler(TcpClient client, int count, EventArgs e);
        public event ChapNhanKetNoiHandler ChapNhanKetNoiEvent;
        public delegate void DongKetNoiHandler(TcpClient client, int count, EventArgs e);
        public event DongKetNoiHandler DongKetNoiEvent;
        /// <summary>
        /// Danh sách Client kết nối tới
        /// </summary>
        public List<TcpClient> ListClient
        {
            get { return _listClient; }
            set { _listClient = value; }
        }
        public TcpListener Server
        {
            get { return _server; }
            set { _server = value; }
        }
        #endregion

        public CSocket()
        {
            _listClient = new List<TcpClient>();

            CConfig config = new CConfig();
            try
            {
                _server = new TcpListener(config.Port);
                _server.Start();
                _isConnect = true;
            }
            catch (Exception c)
            {
                throw new Exception("Không thể tạo socket lắng nghe tại port=" + config.Port);
            }
        }

        /// <summary>
        /// Chấp nhận kết nối từ Client
        /// </summary>
        /// <returns></returns>
        public TcpClient ChapNhanKetNoi()
        {
            TcpClient client = null;
            try
            {
                client = _server.AcceptTcpClient();
            }
            catch
            {
            }
            if (client != null)
            {
                ListClient.Add(client);
                if (ChapNhanKetNoiEvent != null)
                {
                    ChapNhanKetNoiEvent(client, _listClient.Count, new EventArgs());
                }
            }

            return client;
        }
        /// <summary>
        /// Đóng kết nối
        /// </summary>
        /// <param name="client"></param>
        public void DongKetNoi(TcpClient client)
        {
            try
            {
                ListClient.Remove(client);

                
                if (client.Connected)
                {
                    if (DongKetNoiEvent != null)
                    {
                        DongKetNoiEvent(client, _listClient.Count, new EventArgs());
                    }
                    client.Close();
                }
            }
            catch (Exception c)
            {
                throw new Exception("không thể đóng kết nối");

            }
        }
        /// <summary>
        /// Đóng tất cả kết nối
        /// </summary>
        public void DongKetNoi_All()
        {
            try
            {
                for (int i = 0; i < _listClient.Count; i++)
                {
                    if (_listClient[i].Connected)
                        DongKetNoi(_listClient[i]);
                }
                _server.Stop();
                _isConnect = false;
            }
            catch (Exception c)
            {
                throw new Exception("không thể đóng kết nối");
            }
        }
        /// <summary>
        /// Đóng tất cả kết nối
        /// </summary>
        public void DongKetNoiServer()
        {
            try
            {
                _server.Stop();
                _isConnect = false;
            }
            catch (Exception c)
            {
                throw new Exception("không thể đóng kết nối");
            }
        }
    }
}


